﻿using DotNetUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DotNetUI.Controls
{
    /// <summary>
    /// 分页控件
    /// </summary>
    public class DotNetPager : ContentControl
    {
        /// <summary>
        /// 分页控件
        /// </summary>
        static DotNetPager()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetPager), new FrameworkPropertyMetadata(typeof(DotNetPager)));
            
        }
        /// <summary>
        /// 页面详细信息
        /// </summary>
        public static readonly DependencyProperty PageDetailVisibilyProperty = DependencyProperty.Register(nameof(PageDetailVisibily),typeof(Visibility),typeof(DotNetPager));
       /// <summary>
       /// 页面详细信息
       /// </summary>
        public Visibility PageDetailVisibily { get=>this.GetValue<Visibility>(PageDetailVisibilyProperty); set=>SetValue(PageDetailVisibilyProperty,value); }
        public DotNetPager()
        {
           FirstPageCommand= new DelegateCommand((s)=>OnGotoPage(1,PageSize), (s)=> 
            {
                if (PageSize>0&&PageIndex>1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            //前一页命令
            PreviewPageCommand= new DelegateCommand((s)=> OnGotoPage(PageIndex-1, PageSize), (s) =>
            {
                if (PageSize > 0 && PageIndex > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            NextPageCommand = new DelegateCommand((s) => OnGotoPage(PageIndex + 1, PageSize), (s) =>
            {
                if (PageSize > 0 && PageIndex <TotalPage)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            LastPageCommand = new DelegateCommand((s) => OnGotoPage(TotalPage, PageSize), (s) => {
                if (PageSize > 0 && PageIndex < TotalPage)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
            GoToPageCommand = new DelegateCommand(s => OnGotoPage(GoToPageIndex, PageSize), (s) => 
            {
                if (PageSize > 0 && GoToPageIndex <= TotalPage&& GoToPageIndex>0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
        }
        /// <summary>
        /// 导航到指定页
        /// </summary>
        public static readonly RoutedEvent GoPageEvent = EventManager.RegisterRoutedEvent(nameof(GoToPage),RoutingStrategy.Bubble,typeof(EventHandler<PageInfoEventArgs>),typeof(DotNetPager));
        /// <summary>
        /// 导航到指定页
        /// </summary>
        public event EventHandler<PageInfoEventArgs> GoToPage
        {
            add
            {
                AddHandler(GoPageEvent,value);
            }
            remove
            {
                RemoveHandler(GoPageEvent,value);
            }
        }

        /// <summary>
        /// 导航到指定页码
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        protected  void OnGotoPage(int pageIndex,int pageSize)
        {
            this.RaiseEvent(new PageInfoEventArgs(pageIndex,pageSize, GoPageEvent));
        }
        /// <summary>
        /// 总页码
        /// </summary>
        public static readonly DependencyPropertyKey TotalPageProperty = DependencyProperty.RegisterReadOnly(nameof(TotalPage),typeof(int),typeof(DotNetPager),new PropertyMetadata(0, (s, e) => 
        {
            if (s is DotNetPager pager)
            {
                pager.UpdatePageInfo();
            }
        }, (s, e) => 
        {
            if (e is int number&&number<0)
            {
                return 0;
            }
            return e;
        }));
        private void UpdatePageInfo()
        {

        }
        /// <summary>
        /// 总页码
        /// </summary>
        public int TotalPage { get=>this.GetValue<int>(TotalPageProperty.DependencyProperty);}
        /// <summary>
        /// 当前页
        /// </summary>
        public static readonly DependencyPropertyKey PageIndexProperty = DependencyProperty.RegisterReadOnly(nameof(PageIndex),typeof(int),typeof(DotNetPager),new PropertyMetadata(1, (s, e) =>
        {
            if (s is DotNetPager pager)
            {
                pager.UpdatePageInfo();
            }
        }, (s, e) =>
        {
            if (e is int number && number <1)
            {
                return 1;
            }
            return e;
        }));
        /// <summary>
        /// 设置页码信息
        /// </summary>
        /// <param name="pageInfo"></param>
        public void SetPageInfo(PageInfo pageInfo)
        {
            SetValue(TotalCountProperty, pageInfo.TotalCount);
            SetValue(TotalPageProperty, pageInfo.TotalCount/this.PageSize);
            SetValue(PageIndexProperty,pageInfo.PageIndex);
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get => this.GetValue<int>(PageIndexProperty.DependencyProperty); }
        /// <summary>
        /// 总条数
        /// </summary>

        public static readonly DependencyPropertyKey TotalCountProperty = DependencyProperty.RegisterReadOnly(nameof(TotalCount),typeof(int),typeof(DotNetPager),new PropertyMetadata(0));
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount { get => this.GetValue<int>(TotalCountProperty.DependencyProperty); }
        /// <summary>
        /// 当前页数量
        /// </summary>
        public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register(nameof(PageSize),typeof(int),typeof(DotNetPager));
        /// <summary>
        /// 单页数量
        /// </summary>
        public int PageSize { get=>this.GetValue<int>(PageSizeProperty); set=>SetValue(PageSizeProperty,value); }
        /// <summary>
        /// 当前页数量
        /// </summary>
        public static readonly DependencyPropertyKey CurrentPageCountProperty = DependencyProperty.RegisterReadOnly(nameof(CurrentPageCount), typeof(int), typeof(DotNetPager),new PropertyMetadata (0));

        /// <summary>
        /// 当前页数量
        /// </summary>
        public int CurrentPageCount { get=>this.GetValue<int>(CurrentPageCountProperty.DependencyProperty);  }
        /// <summary>
        /// 导航到的页码
        /// </summary>
        public int GoToPageIndex { get; set; } = 1;
        /// <summary>
        /// 首页
        /// </summary>
        public ICommand FirstPageCommand { get; private set; }
        /// <summary>
        /// 前一页
        /// </summary>
        public ICommand PreviewPageCommand { get; private set; }
        /// <summary>
        /// 下一页
        /// </summary>
        public ICommand NextPageCommand { get; private set; }
        /// <summary>
        /// 最后一页
        /// </summary>
        public ICommand LastPageCommand { get; private set; }
        /// <summary>
        /// 导航到指定页
        /// </summary>
        public ICommand GoToPageCommand { get; private set; }
    }
    public class PageInfo
    {
        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount { get; set; }
        /// <summary>
        /// 当前页数量
        /// </summary>
        public int CurrentPageCount { get; set; }
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get; set; }
    }
}
